package com.huixuebao.after.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixuebao.after.dto.PolicyOrderDTO;
import com.huixuebao.after.entity.PolicyOrderEntity;
import com.huixuebao.after.mapper.PolicyMapper;
import com.huixuebao.after.service.PolicyService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class PolicyServiceImpl implements PolicyService {

    @Autowired
    PolicyMapper policyMapper;
    /**
     * 获取保单列表
     * @param current
     * @param size
     * @param orderId
     * @param merchantsName
     * @param courseName
     * @param phone
     * @return
     */
    @Override
    public ApiResult getPolicyList(Integer current, Integer size, String orderId, String merchantsName, String courseName, String phone) {
        ApiResult apiResult = new ApiResult();
        try{
            if (ObjectUtil.isNotNull(current) && ObjectUtil.isNotNull(size)){
                IPage<PolicyOrderEntity> page = new Page<>(current,size);
                Map<String,Object> map = new HashMap<>();
                map.put("orderId",orderId);
                map.put("courseName",courseName);
                map.put("phone",phone);
                map.put("merchantsName",merchantsName);
                map.put("orderByField","create_time");
                map.put("orderBySe","asc");
                IPage<PolicyOrderDTO> dtoPage = policyMapper.selectOrders(page,map);
                apiResult.setData(dtoPage);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("查询保单分页列表信息成功!!");
            }else {
                Map<String,Object> map = new HashMap<>();
                map.put("phone",phone);
                map.put("orderId",orderId);
                map.put("courseName",courseName);
                map.put("merchantsName",merchantsName);
                map.put("orderByField","create_time");
                map.put("orderBySe","asc");
                List<PolicyOrderDTO> dtoList = policyMapper.selectOrdersList(map);
                apiResult.setData(dtoList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("查询保单列表成功!!");
            }
        }catch (Exception e){
            log.info("查询保单列表失败!!!");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("查询保单分页列表信息失败!!");
        }
        return apiResult;
    }
}
